package jgjc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class BasicInfoServiceController {

    @Resource
    private RestTemplate restTemplate;

    private String serviceUrl = "http://Basic-Data-Management";

    @GetMapping(value = "/consumer/bridge/bridge/getDeleteStatusList")
    public JSONArray getDeleteStatusList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getDeleteStatusList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getDeckDivideTypeList")
    public JSONArray getDeckDivideTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getDeckDivideTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getCrossTypeList")
    public JSONArray getCrossTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getCrossTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getBridgeCategoryList")
    public JSONArray getBridgeCategoryList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getBridgeCategoryList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getLoadCapacityList")
    public JSONArray getLoadCapacityList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getLoadCapacityList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getDesignLoadList")
    public JSONArray getDesignLoadList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getDesignLoadList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getFunctionTypeList")
    public JSONArray getFunctionTypeList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getFunctionTypeList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getRoadLevelList")
    public JSONArray getRoadLevelList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getRoadLevelList", JSONArray.class);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/getRoadNoList")
    public JSONArray getRoadNoList() {
        JSONArray result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getRoadNoList", JSONArray.class);
        return result;
    }


    @GetMapping(value = "/consumer/bridge/bridge/getBridgePageData")
    public Map<String, Object> getBridgePageData(HttpServletRequest request) {
        Map<String, Object> infoMap = new HashMap<>();
        infoMap.put("pageSize", Integer.valueOf(request.getParameter("pageSize")));
        infoMap.put("currentPage", Integer.valueOf(request.getParameter("currentPage")));
        infoMap.put("name", request.getParameter("name"));
        JSONObject result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/getBridgePageData?pageSize={pageSize}&currentPage={currentPage}&name={name}", JSONObject.class, infoMap);
        return result;
    }

    @GetMapping(value = "/consumer/bridge/bridge/isBridgeExists")
    public boolean isBridgeExists(HttpServletRequest request) {
        boolean result = restTemplate.getForObject(serviceUrl + "/bridge/bridge/isBridgeExists/" + request, boolean.class);
        return result;
    }

    @PostMapping(value = "/consumer/bridge/bridge/add")
    public Map<String, Object> add(@RequestBody Map<Object, Object> infoMap) {
        JSONObject result = restTemplate.postForObject(serviceUrl + "/bridge/bridge/add/", infoMap, JSONObject.class);
        return result;
    }


    @PostMapping(value = "/consumer/bridge/bridge/edit")
    public Map<String, Object> edit(@RequestBody Map<Object, Object> infoMap) {
        JSONObject result = restTemplate.postForObject(serviceUrl + "/bridge/bridge/edit/", infoMap, JSONObject.class);
        return result;
    }

    @DeleteMapping(value = "/consumer/bridge/bridge/deleteBridge")
    public Map<String, Object> deleteBridge(@RequestBody Map<String, Object> requestMap) {
        ResponseEntity<JSONObject> result = restTemplate.exchange(serviceUrl + "/bridge/bridge/deleteBridge?id=" + requestMap.get("id") + "&deleteStatusId=" + requestMap.get("deleteStatusId"), HttpMethod.DELETE, null, JSONObject.class);
        return result.getBody();
    }
}
